import 'package:get/get.dart';
import '../../../../app_session.dart';
import '../../../data/user_profile_entity.dart';
import '../models/workbench_models.dart';

class WorkbenchController extends GetxController {
  // 工作台数据
  final Rx<WorkbenchModel?> workbenchData = Rx<WorkbenchModel?>(null);

  @override
  void onInit() {
    super.onInit();
    _loadWorkbenchData();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  Future<void> _loadUserInfo() async {
    UserProfileEntity? userProfile = await app.getUserFromServer();
    await app.getUserVipEntity();
    userProfile ??= app.getUser();
    userProfile ??= UserProfileEntity();
    workbenchData.value?.userProfile = userProfile;
  }

  /// 加载工作台数据
  Future<void> _loadWorkbenchData() async {
    try {
      _loadUserInfo();
      // 模拟数据
      workbenchData.value = WorkbenchModel(
        userProfile: app.getUser() ?? UserProfileEntity(),
        actionCards: [
          ActionCard(
            backgroud: 'assets/images/work/work_card1_bg.png',
            title: '发布采购信息',
            icon: 'assets/images/work/work_card_icon1.png',
            color: '#FF6B35',
            action: null,
          ),
          ActionCard(
            backgroud: 'assets/images/work/work_card2_bg.png',
            title: '我要发货',
            icon: 'assets/images/work/work_card_icon2.png',
            color: '#4A90E2',
            action: null,
          ),
        ],
        functionSections: [
          FunctionSection(
            title: '商机线索',
            items: [
              FunctionItem(
                id: 'procurement_inquiry',
                title: '采购寻单',
                icon: 'assets/images/work/work_list_icon1.png',
                unreadCount: 186,
                action: null,
              ),
              FunctionItem(
                id: 'missed_calls',
                title: '未接来电',
                icon: 'assets/images/work/work_list_icon2.png',
                unreadCount: 12,
                action: null,
              ),
              FunctionItem(
                id: 'shopping_cart',
                title: '购物车',
                icon: 'assets/images/work/work_list_icon3.png',
                unreadCount: 0,
                action: null,
              ),
              FunctionItem(
                id: 'add_product',
                title: '添加商品',
                icon: 'assets/images/work/work_list_icon4.png',
                unreadCount: 0,
                action: null,
              ),
              FunctionItem(
                id: 'consignment_products',
                title: '代销商品',
                icon: 'assets/images/work/work_list_icon5.png',
                unreadCount: 4,
                action: null,
              ),
              FunctionItem(
                id: 'communication_link',
                title: '通讯链接',
                icon: 'assets/images/work/work_list_icon1.png',
                unreadCount: 0,
                action: null,
              ),
            ],
          ),
          FunctionSection(
            title: '订单管理',
            items: [
              FunctionItem(
                id: 'mall_orders',
                title: '商城订单',
                icon: 'assets/images/work/work_list_icon2.png',
                unreadCount: 1,
                action: null,
              ),
              FunctionItem(
                id: 'procurement_orders',
                title: '采购订单',
                icon: 'assets/images/work/work_list_icon3.png',
                unreadCount: 0,
                action: null,
              ),
              FunctionItem(
                id: 'service_orders',
                title: '服务订单',
                icon: 'assets/images/work/work_list_icon4.png',
                unreadCount: 0,
                action: null,
              ),
            ],
          ),
          FunctionSection(
            title: '展会管理',
            items: [
              FunctionItem(
                id: 'exhibition_live',
                title: '展会直播',
                icon: 'assets/images/work/work_list_icon5.png',
                unreadCount: 0,
                action: null,
              ),
              FunctionItem(
                id: 'exhibition_management',
                title: '展会管理',
                icon: 'assets/images/work/work_list_icon1.png',
                unreadCount: 0,
                action: null,
              ),
              FunctionItem(
                id: 'booth_purchase',
                title: '展位购买',
                icon: 'assets/images/work/work_list_icon2.png',
                unreadCount: 7,
                action: null,
              ),
              FunctionItem(
                id: 'live_stream_link',
                title: '直播间链接',
                icon: 'assets/images/work/work_list_icon3.png',
                unreadCount: 0,
                action: null,
              ),
            ],
          ),
        ],
      );
    } catch (e) {
      print('加载工作台数据失败: $e');
    } finally {}
  }

  /// 刷新数据
  Future<void> refreshData() async {
    await _loadWorkbenchData();
  }

  /// 点击功能项
  void onFunctionItemTap(FunctionItem item) {
    if (item.action != null) {
      item.action!();
    }
  }

  /// 点击操作卡片
  void onActionCardTap(ActionCard card) {
    if (card.action != null) {
      card.action!();
    }
  }
}
